import pandas as pd

import DataUtil


class Test:
    def __init__(self):
        self.dataUtil = DataUtil.DataUtil()
        print("测试类启动")

    def get_connection(self):
        print("测试类获取数据库连接")
        return self.dataUtil.get_db_connect()

    def save_data_to_db(self):
        print("测试类保存数据到数据库")
        data = pd.DataFrame({'a': [2, 2222222, 3], 'b': [4, 5, 6]})
        self.dataUtil.save_data_to_db(data, 'test_table1')

    def query_data_from_db(self):
        print("测试类查询数据库数据")
        sql = "SELECT a as A,b as B FROM `test_table1`"
        data = self.dataUtil.query_data_from_db(sql)
        print(data)

    def query_data_from_csv(self):
        print("测试类查询csv数据")
        file_path = '../data/nginx_2024053100.csv'
        data = self.dataUtil.read_data_from_csv(file_path)
        data.columns = ['ip', 'province', 'city', 'access', 'mobile', 'browser', 'gender', 'age', 'method', 'url',
                        'code']
        print(data)

    # 测试将csv文件写入数据库
    def save_from_csv_to_db(self):
        print("测试类将csv文件写入数据库")
        file_path = '../data/nginx_2024053100.csv'
        data = self.dataUtil.read_data_from_csv(file_path)
        data.columns = ['ip', 'province', 'city', 'access', 'mobile', 'browser', 'gender', 'age', 'method', 'url',
                        'code']
        print(data)
        self.dataUtil.save_data_to_db(data, 'test_table2')

    def save_from_csv_to_new_csv(self):
        print("测试类将csv文件写入数据库")
        file_path = '../data/nginx_2024053100.csv'
        data = self.dataUtil.read_data_from_csv(file_path)
        # data.columns = ['ip', 'province', 'city', 'access', 'mobile', 'browser', 'gender', 'age', 'method', 'url',
        #                 'code']
        # print(data)
        self.dataUtil.save_data_to_csv(data, '../data/newFile')


if __name__ == '__main__':
    test = Test()
    # test.save_data_to_db()
    # test.query_data_from_db()
    # test.save_date_to_csv()
    # test.query_data_from_csv()
    # test.save_from_csv_to_db()
    #test.save_from_csv_to_new_csv()
    print("测试类结束")
